package org.ehe.finance.voucher.mapper;

import org.apache.ibatis.annotations.Param;
import org.ehe.common.mybatis.core.mapper.BaseMapperPlus;
import org.ehe.finance.voucher.domain.ErpFinanceVoucherDetail;
import org.ehe.finance.voucher.domain.vo.ErpFinanceVoucherDetailVo;
import org.ehe.finance.voucher.domain.bo.VoucherDetailStatBo;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
/**
 * 记账凭证明细Mapper接口
 * @author :zhangnn
 * @className :ErpFinanceVoucherDetailMapper
 * @description: TODO
 * @date 2025-08-25 13:21:46
 */
public interface ErpFinanceVoucherDetailMapper extends BaseMapperPlus<ErpFinanceVoucherDetail, ErpFinanceVoucherDetailVo> {

    /**
     * 根据凭证ID查询明细列表
     * @param voucherId 凭证ID
     * @return 明细列表
     */
    List<ErpFinanceVoucherDetailVo> selectDetailsByVoucherId(@Param("voucherId") Long voucherId);

    /**
     * 根据凭证ID批量查询明细列表
     * @param voucherIds 凭证ID列表
     * @return 明细列表
     */
    List<ErpFinanceVoucherDetailVo> selectDetailsByVoucherIds(@Param("voucherIds") List<Long> voucherIds);

    /**
     * 计算凭证的借贷方合计
     * @param voucherId 凭证ID
     * @return 合计数据
     */
    VoucherTotalAmount getVoucherTotalAmount(@Param("voucherId") Long voucherId);

    /**
     * 根据科目ID查询明细
     * @param subjectId 科目ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 明细列表
     */
    List<ErpFinanceVoucherDetailVo> selectDetailsBySubject(@Param("subjectId") Long subjectId,
                                                           @Param("startDate") LocalDate startDate,
                                                           @Param("endDate") LocalDate endDate);

    /**
     * 根据辅助核算查询明细
     * @param customerId 客户ID
     * @param supplierId 供应商ID
     * @param employeeId 员工ID
     * @param projectId 项目ID
     * @param departmentId 部门ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 明细列表
     */
    List<ErpFinanceVoucherDetailVo> selectDetailsByAuxiliary(@Param("customerId") Long customerId,
                                                             @Param("supplierId") Long supplierId,
                                                             @Param("employeeId") Long employeeId,
                                                             @Param("projectId") Long projectId,
                                                             @Param("departmentId") Long departmentId,
                                                             @Param("startDate") LocalDate startDate,
                                                             @Param("endDate") LocalDate endDate);

    /**
     * 统计科目发生额
     * @param subjectIds 科目ID列表
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 统计结果
     */
    List<VoucherDetailStatBo> getSubjectAmountStat(@Param("subjectIds") List<Long> subjectIds,
                                                   @Param("startDate") LocalDate startDate,
                                                   @Param("endDate") LocalDate endDate);

    /**
     * 获取最大分录号
     * @param voucherId 凭证ID
     * @return 最大分录号
     */
    Integer getMaxEntryNo(@Param("voucherId") Long voucherId);

    /**
     * 批量删除凭证明细
     * @param voucherId 凭证ID
     * @return 删除数量
     */
    int deleteByVoucherId(@Param("voucherId") Long voucherId);

    /**
     * 批量插入明细
     * @param details 明细列表
     * @return 插入数量
     */
    int batchInsertDetails(@Param("details") List<ErpFinanceVoucherDetail> details);

    /**
     * 检查凭证明细是否平衡
     * @param voucherId 凭证ID
     * @return 是否平衡
     */
    boolean checkVoucherBalance(@Param("voucherId") Long voucherId);

    /**
     * 获取凭证明细汇总
     * @param voucherId 凭证ID
     * @return 汇总数据
     */
    List<VoucherDetailSummary> getVoucherDetailSummary(@Param("voucherId") Long voucherId);

    /**
     * 根据科目查询余额
     * @param subjectId 科目ID
     * @param endDate 截止日期
     * @return 余额
     */
    BigDecimal getSubjectBalance(@Param("subjectId") Long subjectId,
                                 @Param("endDate") LocalDate endDate);

    /**
     * 查询多币种明细
     * @param voucherId 凭证ID
     * @return 多币种明细
     */
    List<ErpFinanceVoucherDetailVo> selectMultiCurrencyDetails(@Param("voucherId") Long voucherId);

    /**
     * 凭证合计金额内部类
     */
    class VoucherTotalAmount {
        private BigDecimal totalDebit;
        private BigDecimal totalCredit;
        private BigDecimal totalForeignDebit;
        private BigDecimal totalForeignCredit;

        // getters and setters
        public BigDecimal getTotalDebit() { return totalDebit; }
        public void setTotalDebit(BigDecimal totalDebit) { this.totalDebit = totalDebit; }
        public BigDecimal getTotalCredit() { return totalCredit; }
        public void setTotalCredit(BigDecimal totalCredit) { this.totalCredit = totalCredit; }
        public BigDecimal getTotalForeignDebit() { return totalForeignDebit; }
        public void setTotalForeignDebit(BigDecimal totalForeignDebit) { this.totalForeignDebit = totalForeignDebit; }
        public BigDecimal getTotalForeignCredit() { return totalForeignCredit; }
        public void setTotalForeignCredit(BigDecimal totalForeignCredit) { this.totalForeignCredit = totalForeignCredit; }
    }
//
    /**
     * 凭证明细汇总内部类
     */
    class VoucherDetailSummary {
        private Long subjectId;
        private String subjectCode;
        private String subjectName;
        private BigDecimal debitAmount;
        private BigDecimal creditAmount;
        private Integer entryCount;

        // getters and setters
        public Long getSubjectId() { return subjectId; }
        public void setSubjectId(Long subjectId) { this.subjectId = subjectId; }
        public String getSubjectCode() { return subjectCode; }
        public void setSubjectCode(String subjectCode) { this.subjectCode = subjectCode; }
        public String getSubjectName() { return subjectName; }
        public void setSubjectName(String subjectName) { this.subjectName = subjectName; }
        public BigDecimal getDebitAmount() { return debitAmount; }
        public void setDebitAmount(BigDecimal debitAmount) { this.debitAmount = debitAmount; }
        public BigDecimal getCreditAmount() { return creditAmount; }
        public void setCreditAmount(BigDecimal creditAmount) { this.creditAmount = creditAmount; }
        public Integer getEntryCount() { return entryCount; }
        public void setEntryCount(Integer entryCount) { this.entryCount = entryCount; }
    }
}
